<template>
	<view>
		<view class="clockin-lists-section">
			<view class="record-items">
				<view class="item" v-for="(item,index) in lists" :key="index">
					<view class="user-info">
						<view class="avatar">
							<image class="image" :src="item.user_info.avatar" mode="widthFix"></image>
						</view>
						<view class="info">
							<view class="nickname">
								{{item.user_info.nickname}}
							</view>
							<view class="finish-total">
								<text>{{item.create_time_str}}</text>
								<text class="dian"> ∙ </text>
								<text>已打卡</text>
								<text>{{item.user_finish_total}}</text>
								<text>天</text>
							</view>
						</view>
						<view v-if="more" class="action" @click="openUserAction(item.uid)">
							<text class="iconfont">&#xe924;</text>
						</view>
					</view>
					
					<view class="content">
						{{item.content}}
					</view>
					<view class="images" v-if="item.images_list.length > 0">
						<uni-grid style="width: 100%;height: 100%;" :column="gridLength(item.images_list)" :showBorder="false">
						    <uni-grid-item style="text-align: center;" v-for="(imgItem,imgIndex) in item.images_list" :key='imgIndex'>
								<image class="images-item" :src="imgItem.thumb" mode="scaleToFill"></image>
						    </uni-grid-item>
						</uni-grid>
					</view>
				</view>
			</view>
			<view class="load-status">
				<uni-load-more iconType="circle" :status="listLoadStatus" />
			</view>
		</view>
		
		<uni-popup ref="openUserAction" type="bottom">
			<view class="user-action-section">
				<view class="user-record" @click="openUserRecord()">查看该用户打卡记录</view>
				<view class="cancel" @click="closeUserAction()">取消</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		props:{
			more: { //是否显示用户更多操作纽
				type: Boolean,
				default: true
			},
			taskId: { //任务ID
				type: Number,
				default: 0
			},
			uid: { //用户ID
				type: Number,
				default: 0
			}
		},
		data() {
			return {
				lists: [], // 打卡记录列表
				page: 1, // 打卡记录列表页码
				listLoadStatus: 'more', // more 加载前 loading 加载中 noMore没有更多
				
				// 当前操作用户ID
				actionUid: 0,
			}
		},
		mounted() {
			//console.log(this.taskId)
			//console.log(this.uid)
			// 获取用户打卡记录数据
			this.getClockinlist()
		},
		methods: {
			/**
			 * 获取打卡内容记录列表
			 */
			getClockinlist() {
				
				this.ListLoadStatus = 'loading'
				let that = this
				let data = {}
					data.action = 'clockin_lists'
					data.task_id = this.taskId
					data.uid = this.uid
					data.page = this.page
				if(this.page == 1){
					// 清空课程列表
					this.lists = []
				}
				//获取首页数据
				let url = '';
					//#ifdef MP-WEIXIN
					data.m = 'muu_classroom'
					url = 'entry/wxapp/clockin'
					//#endif
					//#ifdef H5
					data.m = 'muu_classroom_plugin_clockin'
					url = 'entry/site/clockin'
					//#endif
				this.util.request({
					url : url,
					data : data,
					success:function(res){
						if (res.data.code == 200) {
							let list = res.data.data.data
							if(list.length!==undefined && list.length > 0){
								that.listLoadStatus = 'more'
								//console.log(list)
								that.lists = that.lists.concat(list);
								that.page = that.page + 1
							}else{
								that.listLoadStatus = 'noMore'
							}
						}
					}
				})
			},
			
			gridLength(arr){
				if(arr.length == 1){
					return 1;
				}else if(arr.length == 4 || arr.length == 2){
					return 2;
				}else{
					return 3;
				}
			},
			
			//预览打卡图片
			preview: function(index) {
				let imgs = this.images.map(item => {
					//只返回图片路径
					return item.preview;
				})
				uni.previewImage({
					urls: imgs,
					current: index,
					longPressActions: {
						itemList: ['发送给朋友', '保存图片', '收藏'],
						success: function(data) {
							console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
						},
						fail: function(err) {
							console.log(err.errMsg);
						}
					}
				});
			},
			
			/**
			 * 展开用户操作区
			 */
			openUserAction(uid) {
				this.$refs.openUserAction.open()
				this.actionUid = uid
			},
			
			/**
			 * 关闭用户操作区
			 */
			closeUserAction() {
				this.$refs.openUserAction.close()
			},
			
			/**
			 * 打开用户打卡列表
			 */
			openUserRecord() {
				let page = 'record'
				let task_id = this.taskId
				let uid = this.actionUid
				let url = page + '?task_id=' + task_id + '&uid=' + uid
				console.log(url)
				//指定跳转地址
				uni.navigateTo({
				    url: url
				});
			}
		},

	}
</script>

<style lang="scss">
	page {
		background-color: #f8f8f8;
	}
	
	.clockin-lists-section {
		padding: 20rpx;
		padding-bottom: calc(constant(safe-area-inset-bottom));
		padding-bottom: calc(env(safe-area-inset-bottom));
		>.title {
			font-size: 28rpx;
			font-weight: 600;
			line-height: 42rpx;
		}
		.record-items {
			
			.item {
				border-radius: 20rpx;
				padding: 20rpx;
				background-color: #fff;
				margin-bottom: 20rpx;
				.user-info {
					display: flex;
					.avatar {
						width: 90rpx;
						height: 90rpx;
						border-radius: 50%;
						overflow: hidden;
						.image{
							width: 100%;
						}
					}
					.info {
						flex: 1;
						height: 90rpx;
						padding-left: 20rpx;
						color: #999;
						.nickname {
							padding-top: 10rpx;
							font-size: 24rpx;
							line-height: 35rpx;
						}
						.finish-total {
							font-size: 24rpx;
							line-height: 35rpx;
							padding-bottom: 10rpx;
							.dian {
								padding: 0 10rpx;
								font-size: 42rpx;
								line-height: 35rpx;
							}
						}
					}
					.action {
						width: 80rpx;
						line-height: 90rpx;
						text-align: center;
						.iconfont {
							font-size: 52rpx;
						}
					}
					
				}
				
				.content {
					padding: 20rpx 0;
				}
				
				.images {
					.images-item {
						width: 95%;
						height: 95%;
						border-radius: 7rpx;
						overflow: hidden;
					}
				}
			}
		}
	}
	
	.user-action-section {
		background-color: #fff;
		border-radius: 20rpx 20rpx 0 0;
		padding: 20rpx 0;

		.user-record {
			line-height: 78rpx;
			height: 80rpx;
			text-align: center;
			border-bottom: 2rpx solid #ddd;
		}
		.cancel {
			line-height: 70rpx;
			height: 60rpx;
			border-top: 10rpx solid #f8f8f8;
			text-align: center;
		}
	}
</style>
